package com.share.example.stock.utils;

import com.share.example.stock.model.ak.OpenCloseLowHigh;

import java.math.BigDecimal;

public class CalcUtil {
    static BigDecimal _0 = BigDecimal.ZERO;

    public static boolean isSt(String name) {
        if(name!=null && (name.startsWith("*ST")||name.startsWith("ST"))){
            return true;
        }else {
            return false;
        }
    }

    public static boolean isExit(String name) {
        if(name!=null && name.endsWith("退")){
            return true;
        }else {
            return false;
        }
    }

    public static OpenCloseLowHigh calcPrice(Double open, Double close, Double low, Double high) {
        BigDecimal _2 = new BigDecimal("2");
        OpenCloseLowHigh re = new OpenCloseLowHigh();
        try {
            if (low != null && high != null) {
                BigDecimal _high = new BigDecimal(high);
                BigDecimal _low = new BigDecimal(low);

                BigDecimal high_low = _high.subtract(_low);//减法
                re.setHighSlow(high_low.doubleValue());

                re.setAvghighSlow(_high.add(_low).divide(_2, 4, BigDecimal.ROUND_HALF_UP).doubleValue());

                if (close != null && high_low.compareTo(_0) != 0) {
                    BigDecimal _close = new BigDecimal(close);

                    BigDecimal close_low__high_low = (_close.subtract(_low)).divide(high_low, 4, BigDecimal.ROUND_HALF_UP);
                    re.setCloseSlowDhighSlow(close_low__high_low.doubleValue());

                    BigDecimal close_high__high_low = (_high.subtract(_close)).divide(high_low, 4, BigDecimal.ROUND_HALF_UP);
                    re.setHighScloseDhighSlow(close_high__high_low.doubleValue());
                }

                if (open != null && high_low.compareTo(_0) != 0) {
                    BigDecimal _open = new BigDecimal(open);

                    BigDecimal high_low__open = high_low.divide(_open, 4, BigDecimal.ROUND_HALF_UP);//除法
                    re.setHighSlowDopen(high_low__open.doubleValue());

                    BigDecimal opem_low__high_low = (_open.subtract(_low)).divide(high_low, 4, BigDecimal.ROUND_HALF_UP);
                    re.setOpenSlowDhighSlow(opem_low__high_low.doubleValue());

                    BigDecimal opem_high__high_low = (_high.subtract(_open)).divide(high_low, 4, BigDecimal.ROUND_HALF_UP);
                    re.setHighSopenDhighSlow(opem_high__high_low.doubleValue());
                }

                if (open != null && close != null) {
                    BigDecimal _open = new BigDecimal(open);
                    BigDecimal _close = new BigDecimal(close);
                    re.setAvgOpenclose(_open.add(_close).divide(_2, 4, BigDecimal.ROUND_HALF_UP).doubleValue());

                    re.setCloseSopen(_close.subtract(_open).doubleValue());

                    BigDecimal close_open__open = (_close.subtract(_open)).divide(_open, 4, BigDecimal.ROUND_HALF_UP);//除法
                    re.setCloseSopenDopen(close_open__open.doubleValue());
                }
            }
        } catch (Exception e) {
        }
        return re;
    }

    //赛选超过8p
    public static boolean fiterCloseSopenDopenU(OpenCloseLowHigh cp, double threshold) {
        boolean re = false;
        if (cp != null && cp.getCloseSopenDopen() != null && cp.getCloseSopenDopen() > threshold) {
//            if (cp.getCloseSopen() != null && cp.getCloseSopen() > 0) {
            re = true;
//            }
        }
        return re;
    }
}
